package com.dzl.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.api.pojo.Order;
import com.dzl.api.pojo.WelfareBlueBrothersOrder;
import com.dzl.api.service.BlueBrothersService;
import com.dzl.fulu.sup.open.api.core.utils.JSONUtil;
import com.dzl.supplier.pojo.WelfareSupplier;
import com.dzl.supplier.service.WelfareSupplierService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 虚拟商品（蓝色兄弟）API - 控制层
 * @author WangJian
 * @date 2021/7/13 0013
 */
@Slf4j
@RestController
@RequestMapping("blueBrothers")
public class BlueBrothersController extends BaseController {

    @Autowired
    private BlueBrothersService blueBrothersService;
    @Autowired
    private WelfareSupplierService welfareSupplierService;

    /**
     * 虚拟商品（蓝色兄弟）直充
     * @param order 充值的入参
     * @return com.dzl.util.JsonResult<java.lang.String>
     * @date 2021/7/13 0013 14:24
     * @author wangjian
     */
    @PostMapping("directCharging")
    public JsonResult<Integer> directCharging(@RequestBody Order order) {
        JSONObject jsonobject = JSONObject.parseObject(blueBrothersService.chargingForBlueBrotherOrFuLu(order));
        JsonResult<Integer> result = new JsonResult<>();
        result.setCode(Integer.valueOf(jsonobject.getString("code")));
        result.setMsg(jsonobject.getString("message"));
        return result;
    }

    /**
     * 订单查询接口
     * @param order 订单查询的入参
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     * @date 2021/7/13 0013 14:44
     * @author wangjian
     */
    @PostMapping("queryOrder")
    public JsonResult<WelfareBlueBrothersOrder> queryOrder(@RequestBody Order order) {
        WelfareSupplier welfareSupplier = welfareSupplierService.selectByPrimaryKey(order.getSupplierId());
        order.setMerchantId(welfareSupplier.getMerchantId());
        return JsonResult.success(blueBrothersService.queryOrder(order));
    }

    /**
     * 余额查询接口
     * @param order api接口的入参
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     * @date 2021/7/13 0013 14:44
     * @author wangjian
     */
    @PostMapping("queryBalance")
    public JsonResult<JSONObject> queryBalance(@RequestBody Order order) {
        WelfareSupplier welfareSupplier = welfareSupplierService.selectByPrimaryKey(order.getSupplierId());
        order.setMerchantId(welfareSupplier.getMerchantId());
        JSONObject jsonobject = JSONObject.parseObject(blueBrothersService.queryBalance(order));
        JsonResult<JSONObject> result = new JsonResult<>();
        result.setCode(Integer.valueOf(jsonobject.getString("code")));
        result.setMsg(jsonobject.getString("balance"));
        result.setData(jsonobject);
        return result;
    }

    /**
     * 产品查询接口
     * @param order api接口的入参
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     * @date 2021/7/13 0013 14:44
     * @author wangjian
     */
    @PostMapping("queryProduct")
    public JsonResult<JSONObject> queryProduct(@RequestBody Order order) {
        WelfareSupplier welfareSupplier = welfareSupplierService.selectByPrimaryKey(order.getSupplierId());
        JsonResult<JSONObject> result = new JsonResult<>();
        order.setMerchantId(welfareSupplier.getMerchantId());
        JSONObject jsonobject = JSONObject.parseObject(blueBrothersService.queryProduct(order));
        result.setCode(Integer.valueOf(jsonobject.getString("code")));
        result.setMsg(jsonobject.getString("message"));
        String result1 = jsonobject.getString("result");
        if (EmptyUtils.isNotEmpty(result1)){
            Map<String, String> keyMap = new HashMap<String, String>();
            keyMap.put("product_name", "item_name");
            keyMap.put("face_value", "channel_price");
            keyMap.put("purchase_price", "original_price");
            JSONArray jsonArray = JSONUtil.changeJsonArr(JSONArray.parseArray(result1), keyMap);
            JSONObject json = new JSONObject();
            json.put("products",jsonArray);
            result.setData(json);
        }else {
            result.setData(jsonobject);
        }
        return result;
    }
}
